package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TCircle;
import com.quwan.manager.controller.dto.CircleDto;
import com.quwan.manager.service.ICircleService;
import com.quwan.mapper.TCircleCommentMapper;
import com.quwan.mapper.TCircleMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author laozhang
 * @date 2022/3/17
 */
@Service
@RequiredArgsConstructor
public class CircleServiceImpl implements ICircleService {

    private final TCircleMapper circleMapper;
    private final OssConfig ossConfig;
    private final TCircleCommentMapper circleCommentMapper;

    @Override
    public PageResult<CircleDto> getCircleListPage(int page, int size,String nickName,Long circleTypeId,Long circleNameId, String beginTime,String endTime) {
        if(StringUtils.isNotBlank(endTime)) {
            endTime = endTime+" 23:59:59";
        }
        Page<CircleDto> circlePage = circleMapper.selectCircleDtoList(new Page<>(page, size),nickName,circleTypeId,circleNameId,beginTime,endTime);
        List<CircleDto> reslist = circlePage.getRecords();
        /* 开始遍历 */
        for (CircleDto ban : reslist) {
            if(ban.getImgUrl()!= null && ban.getImgUrl().length()>0){
                ban.setImgUrl(ossConfig.getHostname()+"/"+ban.getImgUrl());
            }
        }
        PageResult<CircleDto> result = new PageResult<CircleDto>()
                .setCurrent(circlePage.getCurrent())
                .setPages(circlePage.getPages())
                .setSize(circlePage.getSize())
                .setTotal(circlePage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    @Transactional
    public boolean delCircle(Long id) {
        circleCommentMapper.delCircleCommentByCircleId(id);
        return circleMapper.deleteById(id) == 1 ;
    }


}
